<?php
/*
*  Created:  2011-03-04
*  管理控制器
*  @Author nic <nic-chen@qq.com>
*/
include('action.abs.php');
class admin_mod extends action {
	function admin_mod() {
		parent :: action();
	}

	function index() {
		$this->_display('index');
	}

	/**
	* 用户登录
	*/
	function login() {
		if ($this->_isPost()) {
			$username = trim(V('p:username'));
			$pwd = trim(V('p:password'));
			$verify_code = strtolower(V('p:verify_code'));

			if (empty($username) || empty($pwd)) {
				exit('{"state":"401", "msg":"帐号或密码错误"}');
			}

			$rs = $rss = '';
			$rss = DR('adminCom.getAdminByName', '', $username);
			
			if (!isset($rss['rst'][0])) {
				exit('{"state":"401", "msg":"帐号或密码错误"}');
			}

			$rs = $rss['rst'][0];
			if ($rs['pwd'] != md5($pwd)) {
				exit('{"state":"401", "msg":'.$rs['pwd'].'"帐号或密码错误"}');
			}
			
			//检查是否启用验证码
			if(IS_USE_CAPTCHA) {
				$autocode = APP :: N('SimpleCaptcha');
				if (!$autocode->checkAuthcode($verify_code)) {
					exit('{"state":"402", "msg":"验证码错误"}');
				}
			}

			USER::set('__CLIENT_ADMIN_ROOT', $rs['isroot']);	//设置管理员权限
			USER::set('name', $rs['name']);	//设置管理员权限
			USER::aid($rs['id']);			
			exit('{"state":"200"}');
		}

		$this->_display('login');
	}
		
	/**
	* 退出登录
	*/
	function logout() {
		USER::aid('');
		USER::set('__CLIENT_ADMIN_ROOT', '');
		//USER::resetInfo();
		APP :: redirect('mgr/admin.login', 2);
	}

    /*
    * 管理员列表
    */
	function userlist() {

		$page = (int)V('g:page', 1);
		$each = (int)V('g:each', 15);
		$offset = ($page -1) * $each;
		$num = ($page -1) * $each;
		$username = V('g:keyword', '');

		$rss = $rs = '';
        //获取管理员数量
        $count = DR('adminCom.getAdminNum');

		$pager = APP :: N('pager');
		$page_param = array('currentPage'=> $page, 'pageSize' => $each, 'recordCount' => $count['rst'], 'linkNumber' => 10);
		$pager->setParam($page_param);
		TPL :: assign('pager', $pager->makePage());

        //获取管理员基本信息
        $rss = DR('adminCom.getAdminByName', '', $username, $offset, $each);

		//获取当前操作者的数据
		//$p = DR('adminCom.getAdminById', '', $this->_getUid());
 
        //$http_url = 'http://' . V('S:HTTP_HOST') . ':' . V('S:SERVER_PORT') . '/index.php?m=ta&id=';

		TPL :: assign('num', $num);
		TPL :: assign('list', $rss['rst']);
		//TPL :: assign('admin', $p['rst']);
		TPL :: display('mgr/admin_list', '', 0, false);
	}

    /*
     * 管理员删除
     */
	function del() {
		$id = V('g:id', 0);
		if ($this->_getUid() == $id) {
			$this->_error('不能对自己进行删除操作', array('userlist'));
		}
		
		$p = DR('adminCom.getAdminById', '', $this->_getUid());	//获取当前操作者的数据
		if(!$p['rst']['isroot']) {
			$this->_error('您无权限删除', array('search'));
		}

		$rs = DR('adminCom.delAdmin', '', $id);
		if ($rs['rst']) {
			$this->_succ('操作已成功', array('userlist'));
		}
		
		$this->_error('删除失败',  array('userlist'));
	}

    /*
     * 管理员修改密码
     */
	function repassword() {
		$rs = DR('adminCom.getAdminById', '', $this->_getUid());	//获取当前操作者的数据
		if ($this->_isPost()) {
			$id = (int)V('p:id', 0);
			$new_pwd = trim(V('p:pwd'));
			$re_pwd = trim(V('p:re_pwd'));

			if (!$new_pwd) {
				$this->_error('请输入新密码', URL('mgr/admin.repassword', 'id='. $id));
			}
			if ($new_pwd !== $re_pwd) {
				$this->_error('两次输入的新密码不一致', URL('mgr/admin.repassword', 'id='. $id));
			}

			$p = DR('adminCom.getAdminById', '', $id);
			//$rs = DR('adminCom.getAdminById', '', $this->_getUid());	//获取当前操作者的数据
			if (!$p['rst']) {
					$this->_error('不存在的用户', URL('mgr/admin.repassword', 'id='. $id));
			}

			//判断当前操作者是否为超级管理员或本人
			if($rs['rst']['isroot'] || $rs['rst']['id'] == $id) {
				$data = array(
						'pwd' => md5($new_pwd)
				);
				$rs = DR('adminCom.saveAdminById', '', $data, $id);
				
				if (!$rs['rst']) {
					$this->_error('修改密码失败.', URL('mgr/admin.repassword', 'id='. $id));
				}
				$this->_succ('操作已成功', array('userlist'));
			}else{
				$this->_error('您无权限修改', URL('mgr/admin.repassword', 'id='. $id));
			}
		}

		TPL :: assign('info', $rs['rst']);
		TPL :: display('mgr/admin_change_password', '', 0, false);

	}

   /*
    * 管理员添加
    */
	function add() {
		$id = intval(V('g:id'));
		$rs = DR('adminCom.getAdminById', '', $id);	//获取数据
		if ($this->_isPost()) {
			$id = intval(V('p:id'));
			$name = trim(V('p:name'));
			$pwd = trim(V('p:pwd'));
			if (empty($name)) {
				$this->_error('帐号不能为空', array('add'));
			}

			$p = DR('adminCom.getAdminById', '', $this->_getUid());	//获取当前操作者的数据
			if(!$p['rst']['isroot']) {
				$this->_error('您无权限添加', array('add'));
			}
									
			$data = array(
							'name' => $name,
							'addtime' =>APP_LOCAL_TIMESTAMP,
							'isroot' => 0
						);
			if($pwd) $data['pwd'] = md5($pwd);
			$rs = DR('adminCom.saveAdminById', '', $data,$id);
			if ($rs['rst']) {
				$this->_succ('操作已成功', array('userlist'));
			}
			$this->_error('添加失败', array('add'));
		}
		TPL :: assign('info', $rs['rst']);
		TPL :: display('mgr/admin_add', '', 0, false);

	}
}
